package com.study.controller;

import com.study.pojo.RequestMessage;
import com.study.pojo.ResponseResult;
import com.study.service.WeChatService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("WeChat")
public class WeChatController {
    @Resource
    private WeChatService weChartService;

    @RequestMapping(value ="getTestToken", method = RequestMethod.GET)
    public String getTestToken(String signature, String timestamp, String nonce, String echostr) {
        return echostr;
    }

    @RequestMapping(value = "getRequest",method = RequestMethod.POST,produces = "application/xml;charset=UTF-8")
    public String getRequest(@RequestBody RequestMessage message) {
        return weChartService.getXml(message.getMsgType(),message);
    }
    @RequestMapping("getOpenId")
    public ResponseResult getOpenId(@RequestBody Map params){
        if(params.get("code")==null || StringUtils.isEmpty(params.get("code"))){
            ResponseResult result=new ResponseResult();
            result.setCode(400);
            result.setMessage("获取用户ID失败");
            return result;
        }
        return weChartService.getOpenId(params.get("code").toString());
    }
    @RequestMapping(value = "getUserInfo",method = RequestMethod.POST)
    public ResponseResult getUserInfo(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getUserInfo(params.get("openId").toString());
    }
    @RequestMapping(value = "getHomeData",method = RequestMethod.POST)
    public ResponseResult getHomeData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getMainData(params);
    }
    @RequestMapping(value = "getHistoryData",method = RequestMethod.POST)
    public ResponseResult getHistoryData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getHistoryData(params);
    }
    @RequestMapping(value = "getTodayWeight",method = RequestMethod.POST)
    public ResponseResult getTodayWeight(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getTodayWeight(params);
    }
    @RequestMapping(value = "saveWeight",method = RequestMethod.POST)
    public ResponseResult saveWeight(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.saveWeight(params);
    }
    @RequestMapping(value = "getSportsData",method = RequestMethod.POST)
    public ResponseResult getSportsData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getSportsData(params);
    }
    @RequestMapping(value = "getFirstChartData",method = RequestMethod.POST)
    public ResponseResult getFirstChartData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getFirstChartData(params);
    }
    @RequestMapping(value = "getSecondChartData",method = RequestMethod.POST)
    public ResponseResult getSecondChartData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getSecondChartData(params);
    }
    @RequestMapping(value = "getThirdChartData",method = RequestMethod.POST)
    public ResponseResult getThirdChartData(@RequestBody Map params){
        ResponseResult result=checkUserInfo(params);
        if(result!=null){
            return result;
        }
        return weChartService.getThirdChartData(params);
    }

    private ResponseResult checkUserInfo(Map params){
        if(params.get("openId")==null || StringUtils.isEmpty(params.get("openId"))){
            ResponseResult result=new ResponseResult();
            result.setCode(400);
            result.setMessage("用户openId不存在");
            return result;
        }else {
            return null;
        }
    }
}
